//
// Created on 2024/9/10.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#include "functional_observer.h"
#include "api/make_ref_counted.h"
#include "rtc_base/ref_counted_object.h"
#include "rtc_client/rtc_log.h"

namespace ohosrtc {

FunctionalCreateSdpObserver::
FunctionalCreateSdpObserver(
        std::function<void(SDP *)> on_success,
        std::function<void(const std::string &)> on_failure)
        : on_success_(std::move(on_success)), on_failure_(std::move(on_failure)) {}

rtc::scoped_refptr<FunctionalCreateSdpObserver>
FunctionalCreateSdpObserver::Create(
        std::function<void(SDP *)> on_success,
        std::function<void(const std::string &)> on_failure) {
  return rtc::make_ref_counted<FunctionalCreateSdpObserver>(std::move(on_success), std::move(on_failure));
}

void FunctionalCreateSdpObserver::OnSuccess(SDP *desc) {
  if (on_success_ != nullptr) {
    on_success_(desc);
  }
}

void FunctionalCreateSdpObserver::OnFailure(RTCError error) {
  if (on_failure_ != nullptr) {
    on_failure_(error.message());
  }
}

FunctionalSetSdpObserver::FunctionalSetSdpObserver(std::function<void()> on_success,
                                                   std::function<void(const std::string &)> on_failure)
    : on_success_(std::move(on_success)), on_failure_(std::move(on_failure)) {}

rtc::scoped_refptr<FunctionalSetSdpObserver>
FunctionalSetSdpObserver::Create(std::function<void()> on_success,
                                 std::function<void(const std::string &)> on_failure) {
  return rtc::make_ref_counted<FunctionalSetSdpObserver>(std::move(on_success), std::move(on_failure));
}

void FunctionalSetSdpObserver::OnSuccess() {
  if (on_success_ != nullptr) {
    on_success_();
  }
}

void FunctionalSetSdpObserver::OnFailure(RTCError error) {
  if (on_failure_ != nullptr) {
    on_failure_(error.message());
  }
}

FunctionalSetLocalDescriptionObserver::FunctionalSetLocalDescriptionObserver(std::function<void(const webrtc::RTCError)> on_completion)
    : on_completion_(std::move(on_completion)) {}

rtc::scoped_refptr<FunctionalSetLocalDescriptionObserver>
FunctionalSetLocalDescriptionObserver::Create(std::function<void(const webrtc::RTCError)> on_completion) {
  return rtc::make_ref_counted<FunctionalSetLocalDescriptionObserver>(std::move(on_completion));
}

void FunctionalSetLocalDescriptionObserver::OnSetLocalDescriptionComplete(webrtc::RTCError error) {
  if (on_completion_ != nullptr) {
    on_completion_(error);
  }
}

FunctionalSetRemoteDescriptionObserver::FunctionalSetRemoteDescriptionObserver(std::function<void(const webrtc::RTCError)> on_completion)
    : on_completion_(std::move(on_completion)) {}

rtc::scoped_refptr<FunctionalSetRemoteDescriptionObserver>
FunctionalSetRemoteDescriptionObserver::Create(std::function<void(const webrtc::RTCError)> on_completion) {
  return rtc::make_ref_counted<FunctionalSetRemoteDescriptionObserver>(std::move(on_completion));
}

void FunctionalSetRemoteDescriptionObserver::OnSetRemoteDescriptionComplete(webrtc::RTCError error) {
  if (on_completion_ != nullptr) {
    on_completion_(error);
  }
}

} // namespace ohosrtc
